from django.db import models
from django.db.models import get_model
from django.contrib.auth.models import User
from django.contrib import admin

# Create your models here.
class categories(models.Model):
    name = models.CharField(max_length=32)
    weight = models.IntegerField(default=0)
    class Meta:
        ordering = ['weight']
class categoriesAdmin(admin.ModelAdmin):
    list_display = ('name',)
    
class sub_categories(models.Model):
    category = models.ForeignKey(categories, related_name="subcategories")
    name = models.CharField(max_length=32)
    weight = models.IntegerField(default=0)
    class Meta:
        ordering = ['weight']

class sub_categoriesAdmin(admin.ModelAdmin):
    list_display = ('name',)
    
class products(models.Model):
    subcategory = models.ForeignKey(sub_categories)
    name = models.CharField(max_length=64)
    price = models.FloatField()
    shortDescription = models.CharField(max_length=64)
    description = models.TextField()
    image = models.ImageField(upload_to='upload/product_images/')
    changed = models.DateTimeField(auto_now=True,auto_now_add=True)
    status = models.IntegerField(default=0)
    recommended = models.BooleanField()
    class Meta:
        ordering = ['-changed']
    
class productsAdmin(admin.ModelAdmin):
    list_display = ('name',)
    
class inventory_records(models.Model):
    product = models.OneToOneField(products,primary_key=True)
    total = models.IntegerField(default=0)
    available = models.IntegerField(default=0)
    sales_history = models.IntegerField(default=0)
class inventory_recordsAdmin(admin.ModelAdmin):
    list_display = ('product','total','available','sales_history',)

class product_rates_value(models.Model):
    product = models.OneToOneField(products, primary_key=True)
    value = models.FloatField(default=0)
    count = models.IntegerField(default=0) 
    changed = models.DateTimeField(auto_now=True,auto_now_add=True)

class product_favourite(models.Model):
    user = models.ForeignKey(User, related_name="favourite_user")
    product = models.ForeignKey(get_model('products','products'), related_name="favourite_products")
    added = models.DateTimeField(auto_now=True,auto_now_add=True)
    
    def product_id(self):
        return self.product.id
    
    def name(self):
        return self.product.name
    
    def image(self):
        return self.product.image
    
    def price(self):
        return self.product.price
    
admin.site.register(categories,categoriesAdmin)
admin.site.register(sub_categories,sub_categoriesAdmin)
admin.site.register(products,productsAdmin)
admin.site.register(inventory_records,inventory_recordsAdmin)
